#region Header

// MS codename Velocity Distributed Cache Extensions.
// (c) 2009

#endregion


#region Usings

using System;
using System.Globalization;

using Microsoft.Data.Caching;

#endregion


namespace Caching.Extensions.Core
{
    /// <summary>
    /// Inprocess cache item
    /// </summary>
    public sealed class SmartCacheItem
    {
        /// <summary>
        /// Gets or sets the cached item.
        /// </summary>
        /// <value>The item.</value>
        public object Item { get; internal set; }
        /// <summary>
        /// Gets or sets the cache item version.
        /// </summary>
        /// <value>The version.</value>
        public DataCacheItemVersion Version { get; internal set; }
        internal DateTime ExpirationTimeUtc { get; set; }


        /// <summary>
        /// Initializes a new instance of the <see cref="SmartCacheItem"/> class.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="version">The version.</param>
        /// <param name="expirationTime">The expiration time.</param>
        public SmartCacheItem(object item, DataCacheItemVersion version, DateTime expirationTime)
        {
            Item = item;
            Version = version;
            ExpirationTimeUtc = expirationTime;
        }


        /// <summary>
        /// Determines whether item can be considered as expired at specified time.
        /// </summary>
        /// <param name="utcTime">The UTC time.</param>
        /// <returns>
        /// 	<c>true</c> if item will be considered as expired at specified time; otherwise, <c>false</c>.
        /// </returns>
        public bool IsExpired(DateTime utcTime)
        {
            return utcTime >= ExpirationTimeUtc;
        }


        public override string ToString()
        {
            var itemDescr = Item != null ? Item.GetType().FullName : string.Empty;
            return string.Format(CultureInfo.InvariantCulture,
                "Item:[{0}], expire:[{1:s}], v.[{2}]", itemDescr, ExpirationTimeUtc, Version);
        }
    }
}